import axios from 'axios'
import * as contents from './constents'
import { fromJS } from 'immutable'


const changeHomeData = (data) => {
  return {
    type: contents.CHANGE_HOME_DATA,
    topicList: data.topicList,
    articleList: data.articleList,
    recommendList: data.recommendList
  }
}

const addHomeInfo = (list, articlePage) => {
  return {
    type: contents.ADD_ARTICLE_LIST,
    list: fromJS(list),
    articlePage
  }

}


export const getHomeInfo = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then(res => {
      const { data } = res.data
      const action = changeHomeData(data)
      dispatch(action)

    })
  }
}

export const getMoreList = (articlePage) => {
  return (dispatch) => {
    axios.get('/api/homeList.json?page' + articlePage).then(res => {
      const { data } = res.data
      const action = addHomeInfo(data, articlePage + 1)
      dispatch(action)

    })
  }
}


export const toggleToShow = (show) => ({
  type: contents.TOGGLE_SCROLL_TOP,
  show
})
